package com.nd.qa.shareplate.controller;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.nd.qa.shareplate.common.SessionUserInfo;
import com.nd.qa.shareplate.common.SysConstant;
import com.nd.qa.shareplate.model.ShareTestApk;
import com.nd.qa.shareplate.service.TestApkService;
import com.nd.qa.shareplate.util.JsonUtil;


/**
 * <br>testapk管理类
 * @author huangjian
 * @createDate 2016-06-01 下午 18:08:08
 * @version V1.0
 */

@Controller
@RequestMapping("/testApk/*")
public class TestApkController {

	Logger logger = LoggerFactory.getLogger(TestApkController.class);
	
	@Autowired
	@Qualifier("com.nd.qa.shareplate.service.TestApkService")
	private TestApkService testApkService ;
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/totalTestApk")
	public String totalTestApk(HttpServletRequest request,HttpServletResponse response,@RequestBody String params,Model model,HttpSession session){
		
		SessionUserInfo sessionUserInfo = (SessionUserInfo) session.getAttribute(SysConstant.SESSION_KEY_LOGIN_USER);
		if (sessionUserInfo==null) {
			return "/main/login";
		}
		String userId = sessionUserInfo.getUserId();
		HashMap<String, Object> inParam = JsonUtil.toObject(params, HashMap.class);
		ShareTestApk apk = new ShareTestApk();
		apk.setUserId(userId);
		Integer totalPages = 0;//总记录数
		Integer currentPage = Integer.valueOf(String.valueOf(inParam.get("currentPage")));
		Integer pageSize = Integer.valueOf(String.valueOf(inParam.get("pageSize")));
		Integer startSize = (currentPage-1)*pageSize;
		try {
			List<ShareTestApk> totalApkList = testApkService.totalTestApk(apk);
			if (totalApkList.size()>0&&totalApkList.size()%pageSize==0) {
				totalPages = totalApkList.size()/pageSize;
			}else {
				totalPages = totalApkList.size()/pageSize + 1;
			}
			/*List<Apk> apkList1 = apkService.queryApkByUserId(apk);
			for (int i = 0; i < apkList1.size(); i++) {
				System.out.println(apkList1.get(i).getApkDescription());
				System.out.println(apkList1.get(i).getApkName());
				System.out.println(apkList1.get(i).getSavePath());
				System.out.println(apkList1.get(i).getUserId());
			}*/
			HashMap<String,Object> paramMap = new HashMap<String,Object>();
			paramMap.put("startSize", startSize);
			paramMap.put("pageSize", pageSize);
			paramMap.put("userId", sessionUserInfo.getUserId());
			List<ShareTestApk> apkList = testApkService.queryLimitTestApk(paramMap);
			model.addAttribute("apkList", apkList);
			model.addAttribute("totalPages", totalPages);
		} catch (Exception e) {
			logger.error("查询apk信息异常"+e);
			e.printStackTrace();
		}
		return "/main/testapk";
	}
	
	/**
	 * 删除apk信息
	 * @param request
	 * @param response
	 * @param session
	 * @param params
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/deleteTestApkById")
	public void deleteTestApkById(HttpServletRequest request,HttpServletResponse response,HttpSession session,@RequestBody String params){
		
		SessionUserInfo sessionUserInfo = (SessionUserInfo) session.getAttribute(SysConstant.SESSION_KEY_LOGIN_USER);
		HashMap<String, Object> resultMap = new HashMap<String,Object>();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		if (sessionUserInfo==null) {
			resultMap.put("code", "-2");
			resultMap.put("message", "您暂未登录，请先登录");
		}
		response.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html;charset=UTF-8");
	    HashMap<String, Object> inParam = JsonUtil.toObject(params, HashMap.class);
	    Integer testApkId = Integer.valueOf(String.valueOf(inParam.get("testApkId")));
	    ShareTestApk apk = new ShareTestApk();
	    apk.setTestApkId(testApkId);
	    apk.setUserId(sessionUserInfo.getUserId());
	    try {
	    	testApkService.deleteTestApkById(apk);
			resultMap.put("code", "0");
			resultMap.put("message", "删除apk信息成功");
			response.getWriter().print(JsonUtil.toString(resultMap));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查询测试apk
	 * @param request
	 * @param response
	 * @param session
	 * @param params
	 */
	@RequestMapping("/searchTestApk")
	public String searchTestApk(HttpServletRequest request,HttpServletResponse response,HttpSession session,@RequestBody String params,Model model){
		SessionUserInfo sessionUserInfo = (SessionUserInfo) session.getAttribute(SysConstant.SESSION_KEY_LOGIN_USER);
		HashMap<String, Object> resultMap = new HashMap<String,Object>();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		if (sessionUserInfo==null) {
			resultMap.put("code", "-2");
			resultMap.put("message", "您暂未登录，请先登录");
		}
		response.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html;charset=UTF-8");
	    HashMap<String, Object> inParam = JsonUtil.toObject(params, HashMap.class);
	    Integer totalPages = 0;//总记录数
		Integer currentPage = Integer.valueOf(String.valueOf(inParam.get("currentPage")));
		Integer pageSize = Integer.valueOf(String.valueOf(inParam.get("pageSize")));
		String  scriptName = String.valueOf(inParam.get("scriptText"));
		Integer startSize = (currentPage-1)*pageSize;
		ShareTestApk testApk = new ShareTestApk();
		testApk.setScriptName(scriptName);
		testApk.setUserId(sessionUserInfo.getUserId());
		try {
			List<ShareTestApk> totalApkList = testApkService.searchTestApk(testApk);
			if (totalApkList.size()>0&&totalApkList.size()%pageSize==0) {
				totalPages = totalApkList.size()/pageSize;
			}else {
				totalPages = totalApkList.size()/pageSize + 1;
			}
			HashMap<String,Object> paramMap = new HashMap<String,Object>();
			paramMap.put("startSize", startSize);
			paramMap.put("pageSize", pageSize);
			paramMap.put("userId", sessionUserInfo.getUserId());
			paramMap.put("scriptName", scriptName);
			List<ShareTestApk> apkList = testApkService.queryLimitTestApk(paramMap);
			model.addAttribute("apkList", apkList);
			model.addAttribute("totalPages", totalPages);
		} catch (Exception e) {
			logger.error("查询apk信息异常"+e);
			e.printStackTrace();
		}
		return "/main/testapk";
	}
}
